package org.example.trans.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.trans.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
@RequestMapping("/trans")
@Slf4j
public class TransController {
    @Autowired
    private UserService userService;

    //正常操作没有跑出异常
    @Transactional
    @RequestMapping("/r1")
    public String registry1(String userName, String password) {
        Integer result = userService.insertUser(userName, password);
        log.info("用户插入成功，result："+result);
        return "注册成功";
    }

    //抛出异常,事务回滚,此处异常指运行时异常和error，如果是IOException，事务提交
    //程序报错，且没有处理
    @Transactional
    @RequestMapping("/r2")
    public String registry2(String userName, String password) {
        Integer result = userService.insertUser(userName, password);
        log.info("用户插入成功，result："+result);
        int a=10/0;
        return "注册成功";
    }

    //程序报错了，但是捕获了异常
    //事务提交
    @Transactional
    @RequestMapping("/r3")
    public String registry3(String userName, String password) {
        Integer result = userService.insertUser(userName, password);
        log.info("用户插入成功，result："+result);
        try {
            int a=10/0;
        } catch (Exception e) {
            log.info("程序出错");
        }
        return "注册成功";
    }
    //程序报错，重新抛出
    //事务回滚
    @Transactional
    @RequestMapping("/r4")
    public String registry4(String userName, String password) {
        Integer result = userService.insertUser(userName, password);
        log.info("用户插入成功，result："+result);
        try {
            int a=10/0;
        } catch (Exception e) {
            log.info("程序出错");
            throw e;
        }
        return "注册成功";
    }
    // 程序出错，手动让事务回滚
    //
    @Transactional
    @RequestMapping("/r5")
    public String registry5(String userName, String password) {
        Integer result = userService.insertUser(userName, password);
        log.info("用户插入成功，result："+result);
        try {
            int a=10/0;
        } catch (Exception e) {
            log.info("程序出错");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return "注册成功";
    }

    //如果抛出的为IOException，事务仍提交
    @Transactional
    @RequestMapping("/r6")
    public String registry6(String userName, String password) throws IOException {
        Integer result = userService.insertUser(userName, password);
        log.info("用户插入成功，result："+result);
        try {
            int a=10/0;
        } catch (Exception e) {
            log.info("程序出错");
            throw new IOException();
        }
        return "注册成功";
    }

    @Transactional(rollbackFor = {Exception.class,Error.class})
    @RequestMapping("/r7")
    public String registry7(String userName, String password) throws IOException {
        Integer result = userService.insertUser(userName, password);
        log.info("用户插入成功，result："+result);
        try {
            int a=10/0;
        } catch (Exception e) {
            log.info("程序出错");
            throw new IOException();
        }
        return "注册成功";
    }
}
